package com.chao.gateway.utils;

import cn.hutool.json.JSONUtil;
import com.chao.common.api.ResultData;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * 
 * @Description:   
 * @className: ResponseUtil 
 * @author fu wenchao
 * @date 2022/2/24 17:23 
 * @company:西安博达软件股份有限公司
 * @copyright: Copyright (c) 2022
 * @version V1.0 
 */

public class ResponseUtil {

    public static Mono<Void> response(ServerHttpResponse response, HttpStatus httpStatus,Integer code,String msg){
        response.setStatusCode(httpStatus);
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        ResultData<Object> resultData = ResultData.failed(code, msg);

        String body = JSONUtil.toJsonStr(resultData);
        DataBuffer dataBuffer = response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
        return response.writeWith(Mono.just(dataBuffer));
    }
}
